<?php

    function is_friend($account_id,$friend_id) {
        $CI = & get_instance();
        $query = $CI->db->get_where('doggy_friends', array('account_id'=>$account_id,'friend_id'=>$friend_id));
        if($query->num_rows>0){
            return TRUE;
        }  else {
            return FALSE;
        }
    }
    function count_comment($blog_id){
        $CI = & get_instance();
        $CI->load->model('Blog_m');
        $CI->db->select();
        $CI->db->from('seating_comment');
        $CI->db->where('seating_comment.blog_id ',$blog_id);
        $CI->db->order_by('seating_comment.id desc');
        return count($CI->db->get()->result_array());
    }
    function get_display_name($account_id) {
        $CI = & get_instance();
        $query = $CI->db->get_where('doggy_profile', array('account_id'=>$account_id));
        if($query->num_rows>0){
            $row = $query->row();
            return $row->display_name;
        }  else {
            return FALSE;
        }
    }

    function root_url($baby_name) {
        $CI = & get_instance();
        $base_url = $CI->config->item('base_url');
        $str = array();
        $str = explode($baby_name, $base_url);
        return $str[0];
    }

    function get_date_part($date, $part_to_return='') {
        $date_str = array();
        $date_str = explode('-', $date);
        if ($part_to_return == 'year') {
            return $date_str[0];
        }
        if ($part_to_return == 'month') {
            return $date_str[1];
        }
        if ($part_to_return == 'day') {
            return $date_str[2];
        }
    }

    function delete_pro_file($file_to_delete) {
        if (file_exists($file_to_delete)) {
            unlink($file_to_delete);
        }
    }

    function is_user($go_after_login=TRUE) {
        $CI = & get_instance();
        if ($CI->session->userdata('doogy.id')) {
            return true;
        }
        if ($go_after_login == TRUE) {
            redirect('home');
        }
        return FALSE;
    }

    function is_pro($go_after_login=TRUE) {
        $CI = & get_instance();
        if ($CI->session->userdata('proid')) {
            return true;
        }
        if ($go_after_login == TRUE) {
            redirect('espace_pro/connexion');
        }
        return FALSE;
    }

    function convertdate($string) {
        return implode('/', array_reverse(explode('-', $string)));
    }

    function convertdate2($string) {
        return implode('-', array_reverse(explode('/', $string)));
    }

    function delete_image($image_to_delete, $thumb_to_delete) {
        if (file_exists($image_to_delete)) {
            unlink($image_to_delete);
        }
        if (file_exists($thumb_to_delete)) {
            unlink($thumb_to_delete);
        }
    }

    function get_short_description($desription, $no_of_word) {
        $ret_val = '';
        $str = generate_pure_text($desription, '<p>', '</p>');
        $str_array = array();
        $str_array = explode(' ', $str);
        if ($no_of_word > count($str_array)) {
            return $str;
        }
        for ($i = 0; $i < $no_of_word; $i++) {
            $ret_val.=$str_array[$i] . ' ';
        }
        return $ret_val;
    }

    function generate_pure_text($html_text, $start_element, $end_element) {
        $pure_text = '';
        $str = array();
        $str = explode($start_element, $html_text);
        $first_process_text = '';
        for ($i = 0; $i < count($str); $i++) {
            $first_process_text.=$str[$i];
        }
        $str = explode($end_element, $first_process_text);
        for ($i = 0; $i < count($str); $i++) {
            $pure_text.=$str[$i];
        }
        return $pure_text;
    }

    function is_admin($go_after_login=TRUE) {
        $CI = & get_instance();
        if ($CI->session->userdata('adminid')) {
            return TRUE;
        }
        if ($go_after_login == TRUE) {
            redirect(root_url('seatting-falin') . 'index.php/admin/login');
        }
        return FALSE;
    }

    function is_super_admin($is_normal_admin=TRUE) {
        $CI = & get_instance();
        if ($CI->session->userdata('adminid') && $CI->session->userdata('level') == 1) {
            $admin_data = array
            (
            'adminname' => $CI->session->userdata('adminname'),
            'adminid' => $CI->session->userdata('adminid'),
            'level' => $CI->session->userdata('level'),
            );
            return $admin_data;
        }
        if ($is_normal_admin == TRUE) {
            redirect(root_url('doggy-flirt') . 'index.php/admin');
        }
        return FALSE;
    }

    function form_fckeditor($data = '', $value = '', $extra = '') {
        $CI = & get_instance();
        $fckeditor_basepath = $CI->config->item('fckeditor_basepath');
        require_once( $_SERVER["DOCUMENT_ROOT"] . $fckeditor_basepath . 'fckeditor.php' );
        $instanceName = ( is_array($data) && isset($data['name']) ) ? $data['name'] : $data;
        $fckeditor = new FCKeditor($instanceName);

        if ($fckeditor->IsCompatible()) {
            $fckeditor->Value = $value; //html_entity_decode($value);
            $fckeditor->BasePath = $fckeditor_basepath;
            if ($fckeditor_toolbarset = $CI->config->item('fckeditor_toolbarset_default'))
                $fckeditor->ToolbarSet = $fckeditor_toolbarset;

            if (is_array($data)) {
                if (isset($data['value']))
                    $fckeditor->Value = $data['value']; //html_entity_decode($data['value']);
                if (isset($data['basepath']))
                    $fckeditor->BasePath = $data['basepath'];
                if (isset($data['toolbarset']))
                    $fckeditor->ToolbarSet = $data['toolbarset'];
                if (isset($data['width']))
                    $fckeditor->Width = $data['width'];
                if (isset($data['height']))
                    $fckeditor->Height = $data['height'];
            }
            return $fckeditor->CreateHtml();
        }
        else {
            return form_textarea($data, $value, $extra);
        }
    }

    if (!function_exists('dropdown_data')) {

        function dropdown_data($setting) {
            $CI = & get_instance();
            if (!array_key_exists('order', $setting)) {
                $CI->db->order_by($setting['value_field'], 'asc');
            } else {
                $CI->db->order_by($setting['order']);
            }
            if (isset($setting['where'])) {
                $CI->db->where($setting['where']);
            }

            $query = $CI->db->get($setting['table_name']);
            $options = array();

            if (isset($setting['init_data'])) {
                $options[$setting['init_data']['key']] = $setting['init_data']['name'];
            }

            foreach ($query->result() as $row) {
                $options[$row->$setting['key_field']] = $row->$setting['value_field'];
            }
            return $options;
        }

        function min_form_ckeditor($data) {
            return '<script type="text/javascript" src="' . base_url() . 'system/application/plugins/ckeditor/ckeditor.js"></script>' .
            '<script type="text/javascript">CKEDITOR.replace("' . $data['id'] . '");</script>';
        }

        function form_ckeditor($data) {
            $data['language'] = isset($data['language']) ? $data['language'] : 'es';

            $size = isset($data['width']) ? 'width: "' . $data['width'] . '", ' : '';
            $size .= isset($data['height']) ? 'height: "' . $data['height'] . '", ' : '';

            $options = '{' .
            $size .
            'language: "' . $data['language'] . '",
            stylesCombo_stylesSet: "my_styles",
            startupOutlineBlocks: true,
            entities: false,
            entities_latin: false,
            entities_greek: false,
            forcePasteAsPlainText: false,

            filebrowserImageUploadUrl : "filexplorers/fckeditor_upload/image", // << My own file uploader
            filebrowserImageBrowseUrl : "filexplorers/inlinebrowse/image", // << My own file browser
            filebrowserImageWindowWidth : "80%",
            filebrowserImageWindowHeight : "80%",
            toolbar :[
            ["Source","-","FitWindow","ShowBlocks","-","Preview"],
            ["Undo","Redo","-","Find","Replace","-","SelectAll","RemoveFormat"],
            ["Cut","Copy","Paste","PasteText","PasteWord","-","Print","SpellCheck"],
            ["Form","Checkbox","Radio","TextField","Textarea","Select","Button","ImageButton","HiddenField"],
            ["About"],
            "/",
            ["Bold","Italic","Underline"],
            ["OrderedList","UnorderedList","-","Blockquote","CreateDiv"],
            ["Image","Flash","Table"],

            ["Link","Unlink","Anchor"],
            ["Rule","SpecialChar"],
            ["Styles"]
            ]
            }';


            $my_styles = 'CKEDITOR.addStylesSet("my_styles",
            [
            // Block Styles
            { name : "H3", element : "h3"},
            { name : "Heading 4", element : "h4"},
            { name : "Heading 5", element : "h5"},
            { name : "Heading 6", element : "h6"},
            { name : "Document Block", element : "div"},
            { name : "Preformatted Text", element : "pre"},
            { name : "Address", element : "address"},

            // Inline Styles
            { name: "Centered paragraph", element: "p", attributes: { "class": "center" } },

            { name: "IMG bordered", element: "img", attributes: { "class": "bordered" } },

            { name: "IMG left", element: "img", attributes: { "class": "left" } },
            { name: "IMG right", element: "img", attributes: { "class": "right" } },

            { name: "IMG left bordered", element: "img", attributes: { "class": "left bordered" } },
            { name: "IMGright bordered", element: "img", attributes: { "class": "right bordered" } },
            ]);';
            return
            // fix: move to <HEAD...
            '<script type="text/javascript" src="' . base_url() . 'system/application/plugins/ckeditor/ckeditor.js"></script>' .
            // put the CKEditor
            '<script type="text/javascript">' .
            $my_styles .
            'CKEDITOR.replace("' . $data['id'] . '", ' . $options . ');</script>';
        }

    }
?>